﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace project_forgotten_tuba
{
    public class Player
    {
        public GameState MainGame { get; set; }
        

        public Card SelectedCard { get; set; }

        public Card HeldCard { get; set; }

        public bool DeckLoaded { get; set; }
       


        private int xOffset;
        private int yOffset;
        public void HandleLeftPress(int x, int y)
        {
            List<Card> TempList;

            TempList = MainGame.AllCards.OrderByDescending(c => c.ZLevel).ToList();
            foreach (Card c in TempList)
            {
                if (c.GetSelectionRect().Contains(x, y))
                {
                    if (SelectedCard!=null)
                    {
                        SelectedCard.Highlighted = false;

                    }
                                        

                    SelectedCard =c;
                    SelectedCard.Highlighted = true;


                   

                    
                    HeldCard = c;
                                    
                    xOffset = x - c.X;
                    yOffset = y - c.Y;
                    


                    int maxz = MainGame.AllCards.Select(j => j.ZLevel).Max() + 1;
                    SelectedCard.ZLevel = maxz;
                    
                    break;
                }
            }
        }

     

        public void HandleLeftRelease(int x, int y)
        {
            HeldCard= null;
        }


        public void HandleLeftDrag(int x, int y)
        {
            if (HeldCard != null)
            {
                HeldCard.X = x-xOffset;
                HeldCard.Y = y-yOffset;
            }
        }

        public void HandleTapCard()
        {
            if (SelectedCard != null)
            {
                SelectedCard.Tapped = !SelectedCard.Tapped;
            }
        }


        public void HandleFlipCard()
        {
            if (SelectedCard != null)
            {
                SelectedCard.Flipped = !SelectedCard.Flipped;

            }
        }

        public void PrivateDraw(SpriteBatch Spritejebus)
        { 
        
            if (SelectedCard!= null)
            {

            
                Texture2D maintexture = SelectedCard.CardFront;
                Rectangle mainsrcrect = SelectedCard.RectFront;
                Rectangle maindestrect = new Rectangle(0, 0, GameStatic.CARD_WIDTH*3, GameStatic.CARD_HEIGHT*3);
               


                if (SelectedCard.Flipped || SelectedCard.IsInOpponent())
                {
                    maintexture = SelectedCard.CardBack;
                    mainsrcrect = SelectedCard.RectBack;
                }

                Spritejebus.Draw(maintexture, maindestrect,  Color.White);


                if (MainGame.dict.ContainsKey(SelectedCard.CardName))
                {
                    string SelectedTranslation = MainGame.dict[SelectedCard.CardName];


                    string fulltext = SelectedTranslation;
                    int drawy;
                    int startx;
                    int starty;
                    drawy = 0;
                    startx = 0;
                    starty = 3 * GameStatic.CARD_HEIGHT;
                    while (fulltext.Length > 0)
                    {
                        bool drewtext = false;
                        for (int i = 0; i < fulltext.Length; i++)
                        {
                            if (GameStatic.GameFont.MeasureString(fulltext.Substring(0, i)).X > 2.8*GameStatic.CARD_WIDTH)
                            {
                                Spritejebus.DrawString(GameStatic.GameFont, fulltext.Substring(0, i), new Vector2(startx, starty + drawy), Color.Black);
                                drawy = drawy + (int)GameStatic.GameFont.MeasureString(fulltext.Substring(0, i)).Y;
                                
                                fulltext = fulltext.Substring(i);
                                drewtext = true;
                                break;
                            }
                        }
                        if (drewtext == false)
                        {
                            Spritejebus.DrawString(GameStatic.GameFont, fulltext, new Vector2(startx, starty + drawy), Color.Black);
                            break;
                        }
                    }



                }


            }
        
        }
    }
   
}
